package com.educhains.web.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class PropertyUtils {

    private static final Logger logger = LoggerFactory.getLogger(PropertyUtils.class);

    private static Properties properties = null;

    static {
        loadProperties();
    }

    synchronized static private void loadProperties() {
        properties = new Properties();
        InputStream in = null;
        try {
            in = PropertyUtils.class.getClassLoader().getResourceAsStream("educhains.properties");
            properties.load(in);
        } catch (FileNotFoundException e) {
            logger.error("Failed to find properties. e:", e);
        } catch (IOException e) {
            logger.error("Failed with IOException. e:", e);
        } finally {
            try {
                if (null != in) {
                    in.close();
                }
            } catch (IOException e) {
                logger.error("Failed with IOException. e:", e);
            }
        }
    }

    public static String getProperty(String key) {
        if (properties == null) {
            loadProperties();
        }
        return properties.getProperty(key);
    }

    public static String getProperty(String key, String defaultValue) {
        if (properties == null) {
            loadProperties();
        }
        return properties.getProperty(key, defaultValue);
    }

    public static void main(String[] args) {
        String emailAddress = properties.getProperty("email.address");
        System.out.println(emailAddress);
    }
}
